/*
 * @(#)XfeditRuleListFreeForm.java        1.0 2000/05/09
 *
 * This file is part of Xfuzzy 3.0, a design environment for fuzzy logic
 * based systems.
 *
 * (c) 2000 IMSE-CNM. The authors may be contacted by the email address:
 *                    xfuzzy-team@imse.cnm.es
 *
 * Xfuzzy is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation.
 *
 * Xfuzzy is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */


package xfuzzy.xfedit;

import xfuzzy.lang.*;
import xfuzzy.util.*;
import javax.swing.*;
import javax.swing.table.*;
import java.awt.event.*;

/**
 * Desarrolla una tabla con celdas especiales que permiten editar el antecedente
 * y el consecuente de las reglas en formato libre
 * 
 * @author Francisco Jos� Moreno Velo
 * @modified Pablo Martin Mu�oz
 *
 */
public class XfeditRuleListFreeForm extends JTable implements KeyListener {

	/**
	 * C�digo asociado a la clase serializable
	 */
	private static final long serialVersionUID = 95505666603031L;

	//----------------------------------------------------------------------------//
	//                            MIEMBROS PRIVADOS                               //
	//----------------------------------------------------------------------------//

	/**
	 * Modelo de edici�n de la tabla
	 */
	private XfeditRuleListFreeModel model;

	//----------------------------------------------------------------------------//
	//                                CONSTRUCTOR                                 //
	//----------------------------------------------------------------------------//

	/**
	 * Constructor
	 */
	public XfeditRuleListFreeForm(XfeditRuleListFreeModel model) {
		super(model);
		addKeyListener(this);
		this.model = model;
		setDefaultRenderer(XLabel.class,model);
		setDefaultRenderer(Object.class,model);
		setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		setColumnSelectionAllowed(false);
		setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		getTableHeader().setReorderingAllowed(false);
		getTableHeader().setFont(XConstants.font);
		getColumnModel().getColumn(0).setMinWidth(50);
		getColumnModel().getColumn(0).setMaxWidth(50);
		getColumnModel().getColumn(0).setPreferredWidth(50);
		getColumnModel().getColumn(1).setMinWidth(50);
		getColumnModel().getColumn(1).setMaxWidth(50);
		getColumnModel().getColumn(1).setPreferredWidth(50);
		getColumnModel().getColumn(3).setMinWidth(30);
		getColumnModel().getColumn(3).setMaxWidth(30);
		getColumnModel().getColumn(3).setPreferredWidth(30);
		getColumnModel().getColumn(2).setPreferredWidth(290);
		getColumnModel().getColumn(4).setPreferredWidth(160);
	}

	//----------------------------------------------------------------------------//
	//                             M�TODOS P�BLICOS                               //
	//----------------------------------------------------------------------------//

	/**
	 * Obtiene el editor de una celda de la tabla
	 */
	public TableCellEditor getCellEditor(int row, int column) {
		TableCellEditor editor = model.getEditor(row,column);
		if(editor == null) editor = getDefaultEditor(getColumnClass(column));
		return editor;
	}

	/**
	 * Elimina una fila (regla) de la tabla	
	 */
	public void removeRow() {
		int row = getSelectedRow();
		//No se prodra eliminar ni la primera ni la ultima fila ni la penultima (existe pero no esta editada)
		if(row <0 || row == model.getRowCount()-1) return;
		String msg[] =
		{ "You are going to delete a rule from the rulebase.","Continue?" };
		if(!XDialog.showQuestion(this,msg)) return;
		model.removeRow(row);
	}

	/**
	 * Ejecuta la acci�n sobre la celda seleccionada
	 */
	public void tableAction(int kind) {
		int row = getSelectedRow();
		int column = getSelectedColumn();
		if(row < 0 || row >= model.getRowCount()) return;
		if(column == 4 && kind == -2) model.modelAction(row,-2);
		if(column == 2 && kind > -2) model.modelAction(row,kind);
	}

	/**
	 * Ejecuta la acci�n sobre la celda seleccionada
	 */
	public void tableAction(Variable var, LinguisticLabel pmf) {
		int row = getSelectedRow();
		int column = getSelectedColumn();
		if(row < 0 || row == model.getRowCount()) return;
		if(column == 4) model.changeConclusion(row, var, pmf);
		if(column == 2) model.changePremise(row, var, pmf);
	}

	/**
	 * Interfaz KeyListener. Acci�n al soltar una tecla
	 */
	public void keyReleased(KeyEvent e) {
		int code = e.getKeyCode();
		if(code == KeyEvent.VK_BACK_SPACE) removeRow();
		if(code == KeyEvent.VK_DELETE) removeRow();
		if(code == KeyEvent.VK_CUT) removeRow();
		e.consume();
	}
	
	/**
	 * Interfaz KeyListener. Acci�n al apretar una tecla
	 */
	public void keyPressed(KeyEvent e) {	
	}

	/**
	 * Interfaz KeyListener. Acci�n al teclear.
	 */
	public void keyTyped(KeyEvent e) { 
	}
}


